/**
 * Copyright (c) Salomon Automation GmbH
 */
package ro.cuzma.larry.bookscollection;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.GregorianCalendar;

import ro.cuzma.larry.bookscollection.md.Author;
import ro.cuzma.larry.bookscollection.md.Book;
import ro.cuzma.larry.bookscollection.md.EntityFactory;
import ro.cuzma.larry.bookscollection.md.Library;
import ro.cuzma.larry.bookscollection.persistance.xml.LibraryHelper;
import ro.cuzma.larry.bookscollection.persistance.xml.XMLFactoryLibrary;
import ro.cuzma.larry.persistance.common.Entity;
import ro.cuzma.larry.persistance.common.EntityIdAndValue;
import ro.cuzma.larry.persistance.xml.XMLFileManipulation;
import ro.cuzma.larry.persistance.xml.exception.XMLException;

public class Test {

    /**
     * @param args
     * @throws XMLException
     */
    public static void main(String[] args) throws XMLException {
        Author author = new Author(new Long(1), "first", "middle", "name");
        Author author2 = new Author(new Long(2), "first2", "middle2", "name2");

        EntityIdAndValue<String> lang1 = new EntityIdAndValue<String>("ro", "romana");
        EntityIdAndValue<String> lang2 = new EntityIdAndValue<String>("eng", "english");

        EntityIdAndValue<String> format1 = new EntityIdAndValue<String>("pdf", "pdfee");
        EntityIdAndValue<String> format2 = new EntityIdAndValue<String>("txt", "Text");

        Entity<String> serie1 = new Entity<String>("Fundatia");
        Entity<String> serie2 = new Entity<String>("Robotii");

        Book book = new Book(new Long(2), "bname");
        book.addAuthor(author);
        book.addAuthor(author2);
        book.setLanguage(lang1);

        Book book2 = new Book(new Long(3), "bname2");
        book.setAquireDate(new GregorianCalendar());
        // book2.addAuthor(author2);

        Library lib = new Library("mylib", "c:/test");
        lib.getLanguages().add(lang1);
        lib.getLanguages().add(lang2);
        lib.addAuthor(author);
        lib.addAuthor(author2);
        lib.addBook(book);
        lib.addBook(book2);
        lib.getTypes().add(format1);
        lib.getTypes().add(format2);
        lib.getSeries().add(serie1);
        lib.getSeries().add(serie2);
        LibraryHelper libh = new LibraryHelper(lib);
        System.out.println(libh.toXml(""));

        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("lib.xml"));
            out.write(libh.toXml(""));
            out.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // now open file and read objects from it
        XMLFileManipulation ll = new XMLFileManipulation();
        LibraryHelper lh = (LibraryHelper) ll.loadFromFile(new File("lib.xml"),
                new XMLFactoryLibrary());

        try {
            Library openone = EntityFactory.buildLibrary(lh);
            LibraryHelper lib2 = new LibraryHelper(openone);
            System.out.println(lib2.toXml(""));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
